import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

// local 安装方式
import { VueAccessControl, VueDialog, VueAxios } from '../../../dist/plugins'
import {
    directives,
    utils,
    plugins,
    defineConfig,
    getConfig,
} from '../../../dist'

console.log('[main] utils to', utils.to)

defineConfig({
    oauth2: {
        endpoints: {
            TOKEN_ENDPOINT: '/oauth/token',
        },
        client: {
            client_id: '1000',
            client_secret: 'changeme',
        },
    },
})

const config = getConfig()

console.debug('[sdk] config', JSON.stringify(config.oauth2.client))

// npm 安装方式
// import { VueAccessControl, VueDialog } from 'mc-vue-sdk/dist/plugins'
// import { directives, utils, plugins } from 'mc-vue-sdk'

Vue.config.productionTip = false

console.debug('[sdk]', VueAccessControl, VueDialog, VueAxios)
console.debug('[sdk]', directives, utils, plugins)

/**
 * 令牌失效时回调
 * @returns Promise<any>
 */
function onTokenExpired() {
    return new Promise((resolve) => {
        // todo
        console.debug('开始清理登录信息')
        setTimeout(() => {
            console.debug('结束清理登录信息')
            resolve()
        }, 1000)
    })
}

Vue.use(VueAccessControl, { router, onTokenExpired })
Vue.use(VueDialog)

Object.keys(directives).forEach((key) => {
    console.log('key', key)
    Vue.directive(directives[key].name, directives[key].handler)
})

new Vue({
    router,
    store,
    render: (h) => h(App),
}).$mount('#app')
